#include "timeeditdelegate.h"
#include <QModelIndex>
#include <QPainter>
#include <QDateTime>
#include <QDateTimeEdit>
#include <QStyleOptionViewItemV3>

TimeEditDelegate::TimeEditDelegate(QObject *parent) :
    QItemDelegate(parent)
{
    timeformat="yyyy-MM-dd";
}

QWidget *TimeEditDelegate::createEditor(QWidget *parent,
const QStyleOptionViewItem &/*option*/,
const QModelIndex &/*index*/) const
{
    QDateTimeEdit *editor = new QDateTimeEdit(parent);
    editor->setDisplayFormat(timeformat);
    editor->setCalendarPopup(true);
    editor->installEventFilter(const_cast<TimeEditDelegate*>(this));


    return editor;
}


void TimeEditDelegate::setEditorData(QWidget *editor,
                                 const QModelIndex &index) const
{
   QString dateStr= index.model()->data(index).toString();
   QDate date = QDate::fromString(dateStr,timeformat);

   QDateTimeEdit *edit=static_cast<QDateTimeEdit*>(editor);
   edit->setDate(date);
}


void TimeEditDelegate::setModelData(QWidget *editor,QAbstractItemModel *model,
                                const QModelIndex &index) const
{
    QDateTimeEdit *edit=static_cast<QDateTimeEdit*>(editor);
    QDate date = edit->date();
    model->setData(index,QVariant(date.toString(timeformat)));//Qt::ISODate
}


void TimeEditDelegate::updateEditorGeometry(QWidget *editor,
                                        const QStyleOptionViewItem &option,
                                        const QModelIndex &/*index*/) const
{
    editor->setGeometry(option.rect);
}
void TimeEditDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
                        const QModelIndex &index) const
{
    QDateTime dtval = index.model()->data(index, Qt::DisplayRole).toDateTime();
        QString indexvalue = tr("");
        if(dtval.isValid())
        {
            indexvalue = dtval.toString(timeformat);
        }
        Q_ASSERT(index.isValid());
        QStyleOptionViewItemV3 opt = setOptions(index, option);
        const QStyleOptionViewItemV2 *v2 = qstyleoption_cast<const QStyleOptionViewItemV2 *>(&option);
        opt.features = v2 ? v2->features
                        : QStyleOptionViewItemV2::ViewItemFeatures(QStyleOptionViewItemV2::None);
        const QStyleOptionViewItemV3 *v3 = qstyleoption_cast<const QStyleOptionViewItemV3 *>(&option);
        opt.locale = v3 ? v3->locale : QLocale();
        opt.widget = v3 ? v3->widget : 0;
        // prepare
        painter->save();
        painter->setClipRect(opt.rect);
        // get the data and the rectangles
        QVariant value;
        QPixmap pixmap;
        QRect decorationRect;
        value = index.data(Qt::DecorationRole);
        QString text;
        QRect displayRect;
        value = index.data(Qt::DisplayRole);
        if (value.isValid()) {
            text = indexvalue;
            displayRect = textRectangle(painter, option.rect, opt.font, text);
        }
        QRect checkRect;
        Qt::CheckState checkState = Qt::Unchecked;
        value = index.data(Qt::CheckStateRole);
        if (value.isValid()) {
            checkState = static_cast<Qt::CheckState>(value.toInt());
            checkRect = check(opt, opt.rect, value);
        }
        // do the layout
        doLayout(opt, &checkRect, &decorationRect, &displayRect, false);
        // draw the item
        drawBackground(painter, opt, index);
        drawCheck(painter, opt, checkRect, checkState);
        drawDecoration(painter, opt, decorationRect, pixmap);
        drawDisplay(painter, opt, displayRect, text);
        drawFocus(painter, opt, displayRect);
        // done
        painter->restore();
}
